package com.zhida.blogservice.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.zhida.blogservice.entity.Blog;
import com.zhida.blogservice.entity.LikesLog;
import com.zhida.blogservice.mapper.BlogMapper;
import com.zhida.blogservice.mapper.LikesLogMapper;
import com.zhida.blogservice.service.IBlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhida.common.context.UserContext;
import com.zhida.common.entity.Result;
import com.zhida.common.utils.ObjectUtil;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ling
 * @since 2024-07-29
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {

    @Resource
    BlogMapper blogMapper;
    @Resource
    LikesLogMapper likesLogMapper;
    @Resource
    RabbitTemplate rabbitTemplate;

    @Override
    public Result<?> likeBlog(int blogId) {
        QueryWrapper<LikesLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(Map.of("username", UserContext.getUser() , "obj_id", blogId,"type","blog"));
        LikesLog likesLog = likesLogMapper.selectOne(queryWrapper);
        String resultMsg = "";
        try {
            if (likesLog == null){
                //  插入一条记录
                likesLogMapper.insert(new LikesLog(UserContext.getUser(),blogId,"blog"));
                //  新增一条消息队列，增加blog点赞数
                rabbitTemplate.convertAndSend("zhida.likes", "up", ObjectUtil.Object2JsonStr(blogId));
                resultMsg = "点赞";
            } else {
                UpdateWrapper<LikesLog> updateWrapper = new UpdateWrapper<>();
                updateWrapper.allEq(Map.of("obj_id", blogId, "type", "blog","username",UserContext.getUser()));
                if (likesLog.getStatus() == 0) {
                    //  向消息队列发送一个点赞
                    updateWrapper.set("status", 1);
                    //  更新点赞记录
                    likesLogMapper.update(updateWrapper);
                    //  新增一条消息队列，增加blog点赞数
                    rabbitTemplate.convertAndSend("zhida.likes", "up", ObjectUtil.Object2JsonStr(blogId));
                    resultMsg = "点赞";
                } else {
                    //  向消息队列发送一个取消点赞
                    updateWrapper.set("status", 0);
                    likesLogMapper.update(updateWrapper);
                    //  新增一条消息队列，增加blog点赞数
                    rabbitTemplate.convertAndSend("zhida.likes", "drop", ObjectUtil.Object2JsonStr(blogId));
                    resultMsg = "取消点赞";
                }
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return Result.successMsg(resultMsg);
    }
}
